#include "LED.h"
#include"KEY.h"
uint8 RLedBuf;
uint8 CntLedFlash;
volatile uint16 TimeLedOn;
// const uint8 TAB_TYPE_SEL[] = {0x01,0x03,0x07};
bit F_LED_SEND;
uint8 CntFlashTImes;

const uint8 TAB_LED[] = {0x00,0x01,0x02,0x04,0x08};
void    initLED(void)
{
    SET_IO_LED_MODE();
    DIS_LED1();
    DIS_LED2();
    DIS_LED3();
    DIS_LED4();
    DIS_LED5();

    CLOSE_LED_COM();

    TimeLedOn = 0;
    CntLedFlash = 0;
    F_LED_SEND = 0;
    CntFlashTImes = 0;
}

void    SetLed(uint8 led_buf,uint16 time_on)
{
    TimeLedOn = time_on;

    RLedBuf = led_buf;
    CntLedFlash = 0;

    // CLOSE_LED_COM();

    F_LED_SEND = 1;
    if(time_on)
        CntFlashTImes = 6;
}

void    Led_Main(void)
{    

    SET_IN(L1);
    SET_IN(L2);
    SET_IN(L3);
    SET_IN(L4);

    SET_IO_LED_MODE();

    if(RLedBuf&0x01)    EN_LED1();  
    else                DIS_LED1(); 
    if(RLedBuf&0x02)    EN_LED2();  
    else                DIS_LED2();
    if(RLedBuf&0x04)    EN_LED3();  
    else                DIS_LED3(); 
    if(RLedBuf&0x08)    EN_LED4();  
    else                DIS_LED4(); 


    if((RLedBuf != ALL_ON) && (RLedBuf != ALL_OFF))
    {
        // if(F_LED_SEND)      EN_LED5();      
        // else                DIS_LED5();     
        CntLedFlash++;
        if(CntLedFlash >= 12)
        {
            CntLedFlash = 0;
            if(CntFlashTImes)   
            {
                CntFlashTImes--;
                F_LED_SEND = ~F_LED_SEND;
            }
            if(CntFlashTImes == 0)
                F_LED_SEND = 0;
        }

        // if(F_LED_SEND)  RLedBuf |= 0x10;
        // else            RLedBuf &= ~0x10;
        if(F_LED_SEND)      EN_LED5();      
        else                DIS_LED5();    
    }
    else
    {  
        CntLedFlash = 0;
        F_LED_SEND = 1;    
        if(RLedBuf&0x10)    EN_LED5();      
        else                DIS_LED5(); 
    }
  

    OPEN_LED_COM();

    
    //---- LED 处理 ----//
    if(TimeLedOn)   {TimeLedOn--;}
    else
    {         
        CLOSE_LED_COM();
        DIS_LED1();
        DIS_LED2();
        DIS_LED3();
        DIS_LED4();
        DIS_LED5();
    }
    //------------------//
}


